|
|
|
|
|
|
|
'API's generally require fixed-length strings, which means
'pre-filling them with spaces, nulls, or Dim sName As String * 255
sName = Space$(255)
'Actually call the API
lRetVal = GetComputerName(sName, 255&)
'If the API returns 0 then it didn't work, so exit
If lRetVal = 0 Then Exit Property
'GetComputerName puts a null character, Chr$(0), on the end of the
'string, so we need to remove it before returning the computer name.
iPos = InStr(sName, Chr$(0))
ComputerName = Left$(sName, iPos - 1)
End Property
Public Property Let ComputerName(ByVal sNewName As String)
Dim lRetVal As Long
lRetVal = SetComputerName(sNewName)
RaiseEvent RebootNeeded
End Property |
|
|
|
|
|
|
|
|
In this specific example, the reason that you create the RebootNeeded event is two-fold: First, although the new computer name can be verified in the Control Panel, the GetComputerName API will continue to return the old computer name until you reboot. Second, if you are on a network, you probably will need to reboot to register the new name. In any case, a custom event was used so that the calling program can take the appropriate action. A sample section of code used to test the ComputerInfo class is shown in Listing 20.3. |
|
|
|
|
|
|
|
|
Listing 20.3 COMPNAME.ZIPTesting the ComputerInfo Class from a Form |
|
|
|
|
|
|
|
|
Dim WithEvents clsCompName As ComputerInfo
Sub TestComputerInfoClass()
Dim sName As String
'Create a new instance of the class
Set clsCompName = New ComputerInfo
'Display the computer name
sName = clsCompName.ComputerName
MsgBox "The current computer name is: " & sName
'Set a new Computer name
sName = InputBox$("Enter new computer name")
clsCompName.ComputerName = sName
End Sub
Private Sub clsCompName_RebootNeeded()
MsgBox "You need to reboot to make the changes effective!"
End Sub |
|
|
|
|
|